package cn.signalpha.modules.shop.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.shop.entity.UserAddress;
import cn.signalpha.modules.shop.service.UserAddressService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;


/**
 * 用户收货地址
 *
 * @author suguangjun
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/admin/user/address")
@Api(value = "用户收货地址", tags = "用户收货地址")
public class UserAddressController extends BaseController {

    private final UserAddressService userAddressService;

	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="userId", dataType = "string", value = "用户id"),
	})
	@ApiOperation(value = "分页", notes = "")
	public R<IPage<UserAddress>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		IPage<UserAddress> page = userAddressService.page(Condition.getPage(query), Wrappers.<UserAddress>query().lambda()
			.eq(ObjectUtil.isNotEmpty(params.get("userId")), UserAddress::getUserId, params.get("userId"))
			.orderByDesc(UserAddress::getId)
		);
		return R.data(page);
	}

    @GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
    public R<UserAddress> info(@RequestParam Long id){
        return R.data(userAddressService.getById(id));
    }

	@PostMapping("/submit")
	@ApiOperation("新增或修改")
	public R save(@Valid @RequestBody UserAddress userAddress){
		return R.status(userAddressService.saveOrUpdate(userAddress));
	}

}
